﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace Timesheet
{
    public partial class TestHydra : Form
    {

        //private HydraSystem _hydraSystem;

        public TestHydra()
        {
            InitializeComponent();
        }

        private void btnLogIn_Click(object sender, EventArgs e)
        {
            if(!checkRequiredFields())
            {
                return;
            }

            string usr = this.textBoxUserName.Text.Trim();
            string psw = this.textBoxPassword.Text.Trim();

            HttpHelper httpHelper = new HttpHelper();
            string loginhtml = httpHelper.LogInByOpenair(usr, psw);
            GeneralUtility.writeStringToFile(loginhtml, GeneralUtility.TEST_FILE_3);
            if (!loginhtml.Contains("Log out"))
                throw new Exception("Error happened when log in");
            //step 2 navigate to Timesheets page
            string listHtml = httpHelper.ListTimesheets(loginhtml);
            GeneralUtility.writeStringToFile(listHtml, GeneralUtility.TEST_FILE_4);
            if (string.IsNullOrEmpty(listHtml))
                throw new Exception("Error happened when list timesheets");
            //step 3 verify if have created the timesheet for this week
            //string timesheetForThisWeek = httpHelper.GetTimesheetForDate(weekEnd, listHtml);

            //HttpWebResponse res = Test51CTOSystem.logIn(usr, psw);
            //string htmlString = WebUtility.getHtmlStringFromWebResponse(res);
            //GeneralUtility.writeStringToFile(htmlString, GeneralUtility.TEST_FILE_1);
            //MessageBox.Show("Log in finished!");

            //this._hydraSystem = new HydraSystem(usr, psw);
            //_hydraSystem.Submit();

            //StringBuilder sb = new StringBuilder();
            //sb.Append(this.textBoxUserName.Text);
            //sb.Append("\n");
            //sb.Append("pass:");
            //sb.Append(this.textBoxPassword.Text);
            //GeneralUtility.writeStringToFile(sb.ToString(), GeneralUtility.TEST_FILE_1);
            //MessageBox.Show("Write to file finished!");

        }

        private void btnSendSMS_Click(object sender, EventArgs e)
        {
            string subject = this.textBoxSubject.Text.Trim();
            string content = this.textBoxContent.Text.Trim();

            HttpWebResponse res = Test51CTOSystem.sendMessage(Test51CTOSystem.FRIEND_1_ID, subject, content);
            string htmlString = WebUtility.getHtmlStringFromWebResponse(res);
            GeneralUtility.writeStringToFile(htmlString, GeneralUtility.TEST_FILE_2);
            MessageBox.Show("Send SMS finished!");
        }


        /// <summary>
        /// Check requried fields
        /// </summary>
        /// <returns></returns>
        private bool checkRequiredFields()
        {
            if(string.IsNullOrEmpty(this.textBoxUserName.Text.Trim()))
            {
                MessageBox.Show("Please enter Hydra user name!");
                return false;
            }

            if (string.IsNullOrEmpty(this.textBoxPassword.Text.Trim()))
            {
                MessageBox.Show("Please enter Hydra password!");
                return false;
            }

            return true;
        }

    }
}
